<?php

namespace app\api\controller\ec;

use app\common\controller\Api;
use app\common\exception\UploadException;
use think\Config;
use think\Hook;


/**
 * 公共接口
 */
class Ecuser extends Ecbase {

    public function _initialize() {
        parent::_initialize();
    }
    
    public function groupbyUserIds(){
        $response = $this->ecGroupbyUserIds();
        qqq('groupbyUserIds',$response);
        foreach ($response['data'] as $value){
            $exist_info = db('ec_user')->where('userid',$value['userId'])->find();
            if(!empty($exist_info)){
                db('ec_user')->where('id',$exist_info['id'])->update(['kehu_count'=>$value['num']]);
            }
        }
       
    }
    
    
    public function getAllUser(){
        $response = $this->getStructInfo();
        
        $exist_user_list = db('ec_user')->where('deletetime',0)->column('userid');
        if($response['code'] == 200){
            $user_list = $response['data']['users'];
            $add_data = [];            
            $this->_deal_deparment_list($response['data']['depts']);
            foreach ($user_list as $value){    
                $last_user_list[]  = $value['userId'];
                $exist_info = db('ec_user')->where('userid',$value['userId'])->find();
                if(empty($exist_info)){
                    $add_data[] = [
                        'account'=>$value['account'],
                        'name'=>$value['userName'],
                        'deptId'=>$value['deptId'],
                        'userid'=>$value['userId'],
                        'ec_status'=>$value['status'],
                        'config'=>json_encode($value, JSON_UNESCAPED_UNICODE),
                        'createtime'=>time()
                    ];
                }else{
                    db('ec_user')->where('id',$exist_info['id'])->update([
                        'name'=>$value['userName'],
                        'userid'=>$value['userId'],
                        'deptId'=>$value['deptId'],
                        'ec_status'=>$value['status'],
                        'config'=>json_encode($value, JSON_UNESCAPED_UNICODE),
                        'updatetime'=>time()
                    ]);
                }
            }
            
            foreach ($exist_user_list as $v){
                if(!in_array($v, $last_user_list)){
                    $del_user[] = $v; 
                }
            }
            
            if(!empty($add_data)){
                db('ec_user')->insertAll($add_data);
            }
            if(!empty($del_user)){
                db('ec_user')->where('userid','in',$del_user)->setField('deletetime',time());
            }
            
            $this->success('更新成功');
        }
        
        $this->error('更新失败');
    }
    
    
    public function _deal_deparment_list($list){
        $last_deparment_list = [];
        $exist_deparment_list = db('ec_department')->where('delettime',0)->column('deptId');
        
        
        foreach ($list as $value){
            $last_deparment_list[]  = $value['deptId'];
            $exist_info = db('ec_department')->where('deptId',$value['deptId'])->find();
            if(empty($exist_info)){
                $add_data[] = [
                    'deptId'=>$value['deptId'],
                    'deptName'=>$value['deptName'],
                    'parentDeptId'=>$value['parentDeptId'],
                    'config'=>json_encode($value, JSON_UNESCAPED_UNICODE),
                    'createtime'=>time()
                ];
            }else{
                db('ec_department')->where('deptId',$exist_info['deptId'])->update([
                    'deptName'=>$value['deptName'],
                    'parentDeptId'=>$value['parentDeptId'],
                    'config'=>json_encode($value, JSON_UNESCAPED_UNICODE),
                ]);
            }
        }

        foreach ($exist_deparment_list as $v){
            if(!in_array($v, $last_deparment_list)){
                $del_department[] = $v; 
            }
        }
        
        if(!empty($del_department)){
            db('ec_department')->where('deptId','in',$del_department)->setField('delettime',time());
        }
        
        if(!empty($add_data)){
            db('ec_department')->insertAll($add_data);
        }
    }







    public function batchGetUserId() {

        $list = db('ec_user')->whereNull('mobile')->select();
        foreach ($list as $value) {
            $item_info = $this->getUserInfo($value['userid']);
            if ($item_info['code'] == 200) {
                db('ec_user')->where('id', $value['id'])->update([
                    'mobile' => $item_info['data']['mobile'],
                    'updatetime' => time(),
                ]);
            }
        }
        $this->success('');
    }
    
    
    
    public function getStructInfo() {
        $request_url = 'https://open.workec.com/v2/org/struct/info';
        $data = [];      
        $result  = $this->ecObj->doGet($request_url,$data);
        return $result;
    }
    
    
    public function ecGroupbyUserIds() {
        $request_url = 'https://open.workec.com/v2/statistics/crmStats/groupbyUserIds';
        $data = ['queryType' => 4,"ids"=>[]];
        $request_data = json_encode($data);
        $result = $this->ecObj->doPost($request_url, $request_data);

        return $result;
    }
    
    

    public function getUserInfo($userId) {
        $request_url = 'https://open.workec.com/v2/org/user/findUserInfoById';
        $data = ['userId' => $userId];
        $request_data = json_encode($data);
        $user_info = $this->ecObj->doPost($request_url, $request_data);

        return $user_info;
    }

   
    
}
